package com.example.treehole.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.treehole.bean.Collect;
import com.example.treehole.bean.Post;
import com.example.treehole.bean.User;
import com.example.treehole.common.BaseResult;
import com.example.treehole.service.CollectServiceImpl;
import com.example.treehole.service.PostServiceImpl;
import com.example.treehole.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

// 用于处理点赞相关的请求
@RestController
@RequestMapping("/like")
public class LikeController {
    // 自动注入 CollectService、UserService 和 PostService
    @Autowired
    private CollectServiceImpl collectService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private PostServiceImpl postService;

    // 获取用户点赞的帖子
    @GetMapping("/get")
    public ResponseEntity<?> get(String userid) {
        List<Collect> list = collectService.lambdaQuery()
                .eq(userid != null && !userid.isEmpty(), Collect::getUserid, userid)
                .list();
        return BaseResult.ok("获取成功", list.stream().map(collect -> postService.getById(collect.getTargetId())).filter(new Predicate<Post>() {
            @Override
            public boolean test(Post post) {

                if (!post.anonymous) {
                    User user = userService.getById(post.userid);
                    post.nickname = user.nickname;
                    post.face = user.face;
                    System.out.println(post);
                } else {
                    post.nickname = "匿名用户";
                    post.face = "http://d.hiphotos.bdimg.com/wisegame/pic/item/320fd9f9d72a605946395ca92b34349b033bba5c.jpg";
                }

                return true;
            }
        }).collect(Collectors.toList()));
    }

    // 添加或取消点赞
    @PostMapping("/add")
    public ResponseEntity<?> add(@RequestBody Collect collect) {
        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collect::getTargetId, collect.targetId)
                .eq(Collect::getUserid, collect.userid);
        Collect one = collectService.getOne(queryWrapper);
        Map<String, Boolean> result = new HashMap<>();
        if (one == null) {
            collectService.save(collect);
            result.put("currentStatus", true);
        } else {
            collectService.removeById(one.id);
            result.put("currentStatus", false);
        }
        return BaseResult.ok("提交成功", result);
    }

    // 获取指定用户对指定目标的点赞状态
    @GetMapping("/getStatus")
    public ResponseEntity<?> getStatus(String userid, String targetId, String type) {
        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collect::getTargetId, targetId)
                .eq(Collect::getUserid, userid);
        Collect one = collectService.getOne(queryWrapper);
        Map<String, Boolean> result = new HashMap<>();
        result.put("currentStatus", one != null);
        return BaseResult.ok("获取成功！", result);
    }
}
